package com.yinhe.music.yhmusic.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yinhe.music.yhmusic.login.LoginRegisterActivity;
import com.yinhe.music.yhmusic.utils.StatusBarUtil;

import java.util.List;

import butterknife.Unbinder;

/**
 * @author 何晓华
 */
public abstract class BasePresneterActivity extends BaseActivity implements IBaseView, IServiceConnect  {
    public Unbinder unbinder;

    public int mPage = 1;
    public int mPageSize = 10;

    /**
     * 获取Presenter实例，子类实现
     *
     * @return
     */
    public abstract BasePresenter getPresenter();

    /**
     * 初始化Presenter的实例，子类实现
     */
    public abstract void initPresenter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        initPresenter();
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarTranslucent(this, true);
    }

    @Override
    protected void onDestroy() {
        if (mService != null) mService.removePlayEventListner(this);
        if (getPresenter() != null) {
            getPresenter().onDetach();
            getPresenter().dispose();
        }
        unBindService();
        super.onDestroy();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(Throwable throwable) {
        Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPlayList() {
        playQueueFragment.show(getSupportFragmentManager(), "playlistframent");
    }

    @Override
    public Context getContext() {
        return BasePresneterActivity.this;
    }

    @Override
    public void startLogin() {
        startActivity(new Intent(this, LoginRegisterActivity.class));
    }

    @Override
    public void setLoadMoreList(List list, int pageNum, BaseQuickAdapter mAdapter) {
        int size = list.size();

        if (mPage == 1) {
            mAdapter.setNewData(list);
        }
        if (mPage > 1 && size > 0) {
            mAdapter.addData(list);
        }

        if (size == mPageSize && mPage <= pageNum) {
            mPage++;
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

}
